
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<netdb.h>
#include <string.h>
#define LISTENQ 5
#define MAXLINE 10
#define HOST_ADDR "127.0.0.1"
extern int connfd;
int main()
{
    int listenfd,n,connfd;
    socklen_t len;
    struct sockaddr_in servaddr,cliaddr;
    char buff[MAXLINE];
    listenfd = socket(AF_INET,SOCK_STREAM,0);
    if(listenfd<0)
    {
        printf("socket created failed!\n");
        return -1;
    }
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(8080);
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    if(bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
    {
        printf("bind failed!\n");
        return -1;
    }
    printf("listening ...............\n");
    listen(listenfd,LISTENQ);
    /*
    while(1)
    {
        len = sizeof(cliaddr);
        connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&len);
        inet_aton(HOST_ADDR,&cliaddr.sin_addr.s_addr);
        printf("connect from %s,port %d\n",inet_ntoa(cliaddr.sin_addr.s_addr),ntohs(cliaddr.sin_port));
        ticks = time(NULL);
        sprintf(buff,"% .24s \r \n",ctime(&ticks));
        write(connfd,buff,sizeof(buff));
        close (connfd);
    }*/
    len = sizeof(cliaddr);
    connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&len);
    if((n = read(connfd,buff,MAXLINE))>0)
    {
        buff[n] = 0;
        fputs(buff,stdout);
        printf("oh  this is a test!!");
        printf("\n");
    }
    close (connfd);
    return 0;
}
